﻿using System;
using System.Collections.Generic;
using System.Linq;
using HY.Model;
using Newtonsoft.Json;
using NLog;
using SqlSugar;
using SqlSugar.Extensions;
using WebApi.Infrastructure;
using WebApi.Model;

namespace WebApi.Helpers
{
    public class DbHelper
    {
        //private static SqlSugarClient db;
        //private static void InitDb()
        //{
        //    ////访问MySql
        //    //db = new SqlSugarClient(new ConnectionConfig()
        //    //{
        //    //    ConnectionString = "Server=121.43.163.15;User ID=root;Password=blessme;port=3306;Database=YuXinTest;CharSet=utf8;pooling=true;SslMode=None;",
        //    //    DbType = DbType.MySql,
        //    //    IsAutoCloseConnection = true
        //    //});

        //    ////访问SQL Server
        //    //db = new SqlSugarClient(new ConnectionConfig()
        //    //{
        //    //    ConnectionString = "Server=192.168.1.91;User ID=sa;Password=123;Database=CFYT_DB;Trusted_Connection=False;",
        //    //    DbType = DbType.SqlServer,
        //    //    IsAutoCloseConnection = true
        //    //});

        //    string ConnectionStringParams = AppSetting.GetValue("WebApiParams:ConnectionString");
        //    db = new SqlSugarClient(new ConnectionConfig()
        //    {
        //        ConnectionString = ConnectionStringParams,
        //        DbType = DbType.SqlServer,
        //        IsAutoCloseConnection = true
        //    });

        //    //调试SQL事件，可以删掉
        //    db.Aop.OnLogExecuting = (sql, pars) =>
        //    {
        //        Console.WriteLine(sql); //输出sql,查看执行sql
        //    };
        //}

        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = AppSetting.GetValue("WebApiParams:ConnectionString"),
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true
        },
        db =>
        {
            db.Aop.OnExecutingChangeSql = (sql, pars) =>
            {
                foreach (SugarParameter p in pars)
                {
                    if (p.Value?.GetType() == typeof(string))
                    {
                        p.Value = p.Value.ToString()!.Trim();
                    }
                }
                //logger.Debug("sql:{0},params:{1}", sql, JsonConvert.SerializeObject(pars.ToDictionary(t => t.ParameterName, t => t.Value)));
                return new KeyValuePair<string, SugarParameter[]>(sql, pars);
            };
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                logger.Debug("sql:{0},params:{1}", sql, JsonConvert.SerializeObject(pars.ToDictionary(t => t.ParameterName, t => t.Value)));
            };
        });

        public static SqlSugarScope GetDbConn()
        {
            return Db;
        }

        public static ISimpleClient<Model.RegisterInfo> RegisterInfo => GetDbConn().GetSimpleClient<Model.RegisterInfo>();
        public static ISimpleClient<Model.Users> Users => GetDbConn().GetSimpleClient<Model.Users>();
        public static ISimpleClient<Model.WeightRecord> WeightRecord => GetDbConn().GetSimpleClient<Model.WeightRecord>();
        public static ISimpleClient<Model.Monitor> Monitor => GetDbConn().GetSimpleClient<Model.Monitor>();
        public static ISimpleClient<Model.CheckDevice> CheckDevice => GetDbConn().GetSimpleClient<Model.CheckDevice>();
        public static ISimpleClient<Model.PutOnRecord> PutOnRecord => GetDbConn().GetSimpleClient<Model.PutOnRecord>();
        public static ISimpleClient<Model.PictureModel> PictureModel => GetDbConn().GetSimpleClient<Model.PictureModel>();
        public static ISimpleClient<Model.Log> Log => GetDbConn().GetSimpleClient<Model.Log>();
        public static ISimpleClient<Model.Report> Report => GetDbConn().GetSimpleClient<Model.Report>();
        public static ISimpleClient<Model.SenderList> SenderList => GetDbConn().GetSimpleClient<Model.SenderList>();
        public static ISimpleClient<Model.Config> Config => GetDbConn().GetSimpleClient<Model.Config>();
        public static ISimpleClient<GPutOnRecord> GPutOnRecord => GetDbConn().GetSimpleClient<GPutOnRecord>();
        public static ISimpleClient<YPutOnRecord> YPutOnRecord => GetDbConn().GetSimpleClient<YPutOnRecord>();
        public static ISimpleClient<YRecord> YRecord => GetDbConn().GetSimpleClient<YRecord>();
        public static ISimpleClient<ReceiverList> ReceiverList => GetDbConn().GetSimpleClient<ReceiverList>();
        public static ISimpleClient<TransporterList> TransporterList => GetDbConn().GetSimpleClient<TransporterList>();
        public static ISimpleClient<CustomBaseData> CustomBaseData => GetDbConn().GetSimpleClient<CustomBaseData>();
        public static ISimpleClient<ProductList> ProductList => GetDbConn().GetSimpleClient<ProductList>();
    }
}